package com.future.hedongjun.mall.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.mall.adapter.ShoppingCarAdapter;
import com.future.hedongjun.mall.bean.GoodsDetail;
import com.future.hedongjun.mall.bean.ShoppingCarBean;
import com.future.hedongjun.mall.bean.ShoppingCarResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShoppingCarActivity extends BaseActivity implements ShoppingCarAdapter.onPriceChangedListener, View.OnClickListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.cb_all_check)
    CheckBox cbAllCheck;
    @BindView(R.id.tv_all_price)
    TextView tvAllPrice;
    @BindView(R.id.tv_buy)
    Button tvBuy;
    private ShoppingCarAdapter adapter;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                cbAllCheck.setChecked(true);
            } else if (msg.what == 1) {
                cbAllCheck.setChecked(false);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_car);
        ButterKnife.bind(this);
        setTitle("购物车");
        tvAllPrice.setText("¥0");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ShoppingCarAdapter(initTestData());
        adapter.setPriceChangedListener(this);
        recyclerView.setAdapter(adapter);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        cbAllCheck.setOnClickListener(this);
        tvBuy.setEnabled(false);
        getData();
    }

    private void getData() {
        new HttpRequest<ShoppingCarResponse>()
                .with(this)
                .setApiCode("_myshopcar_001")
                .addParam("userToken", MyApp.getInstance().getUserToken())
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        adapter.setNewData(((ShoppingCarResponse) response).data);
                    }
                }).start(new ShoppingCarResponse());
    }

    public void setAllCheck(boolean b) {
        handler.sendEmptyMessage(b ? 0 : 1);
    }

    private List<ShoppingCarBean> initTestData() {
//        List<ShoppingCarBean> data = new ArrayList<>();
//        for (int i = 1; i < 5; i++) {
//            ShoppingCarBean bean = new ShoppingCarBean();
//            bean.types = "黄色/M码";
//            bean.count = 1;
//            bean.goodsDetail = new GoodsDetail();
//            ArrayList<String> imageUrls = new ArrayList<>();
//            imageUrls.add("http://seopic.699pic.com/photo/50025/2423.jpg_wh1200.jpg");
//            imageUrls.add("http://seopic.699pic.com/photo/50025/2423.jpg_wh1200.jpg");
//            imageUrls.add("http://seopic.699pic.com/photo/50025/2423.jpg_wh1200.jpg");
//            bean.goodsDetail.imgUrls = imageUrls;
//            bean.goodsDetail.goodsTitle = "商品标题商品标题商品标题商品标题商品标题";
//            bean.goodsDetail.goodsPrice = 100.00;
//            data.add(bean);
//        }
        return null;
    }

    @OnClick(R.id.tv_buy)
    public void onViewClicked() {
        List<ShoppingCarBean> data = adapter.getData();

        if (data == null || data.size() == 0) {
            toast("请先选择商品");
            return;
        }
        ArrayList<GoodsDetail> goodsDetails = new ArrayList<>();
        ArrayList<Integer> numList = new ArrayList<>();

        for (int i = 0; i < data.size(); i++) {
            if (!adapter.isChecked.get(i)) {
                break;
            }
            GoodsDetail detail = new GoodsDetail();
            ShoppingCarBean shoppingCarBean = data.get(i);
            detail.setImage(shoppingCarBean.getImg());
            detail.setGoods_name(shoppingCarBean.getShop_name());
//            detail.goodsPrice = shoppingCarBean.getPrice();
            numList.add(shoppingCarBean.getNum());
            goodsDetails.add(detail);
        }
        ConfirmOrderActivity.open(this, goodsDetails, numList);
    }

    @Override
    public void onPriceChanged(double price) {
        if (tvAllPrice != null) {
            if (price <= 0) {
                tvBuy.setEnabled(false);
            } else {
                tvBuy.setEnabled(true);
            }
            tvAllPrice.setText("¥" + price);
        }
    }

    @Override
    public void onClick(View v) {
        if (cbAllCheck.isChecked()) {
            adapter.setAllChecked(true);
        } else {
            adapter.setAllChecked(false);
        }
    }
}
